<?php
namespace app\admin\controller;

use think\facade\View;
use think\App;
use think\Db;
use app\admin\model\HouseProperty as PropertyModel;
use app\admin\model\HouseBilling as BillingModel;
use app\admin\model\BillSum as SumModel;
use app\admin\model\AdminUser as UserModel;

class Index extends Common
{
    protected $checkLoginExclude = ['login', 'logout'];

    public function index()
    {
        return View::fetch();
    }

    public function queryHouse()
    {
        $loginUser = $this->auth->getLoginUser();
        $property_count = PropertyModel::where('admin_user_id', $loginUser['id'])->count();
        $user = UserModel::find($loginUser['id']);
        $number_count =  $user->houseNumber->count();
        $empty_count =  $user->houseNumber->where('rent_mark', 'N')->count();
        $occupancy = $number_count == 0 ? '0%' : round((($number_count - $empty_count)/$number_count)*100).'%';
        $house_info = [
            'property_count' => $property_count,
            'number_count' => $number_count,
            'empty_count' => $empty_count,
            'occupancy' => $occupancy,
            'overdue_todo' => $user->houseBilling->where('accounting_date', null)
                ->where('meter_reading_time', '=', '')
                ->where('start_time', '<', date('Y-m-d H:i:s', time()))
                ->count(),
            'overdue_uncollected' => $user->houseBilling->where('accounting_date', null)
                ->where('meter_reading_time', '!=', '')
                ->where('start_time', '<', date('Y-m-d H:i:s', time()))
                ->count(),
        ];
        return $this->returnElement($house_info);
    }

    public function login()
    {
        if ($this->request->isPost()) {
            $data = [
                'username' => $this->request->post('username/s', '', 'trim'),
                'password' => $this->request->post('password/s', ''),
            ];
            if (!$this->auth->login($data['username'], $data['password'])) {
                $this->error('登陆失败：' . $this->auth->getError() . '。');
            }
            $this->success('登陆成功。');
        }
        View::assign('token', $this->getToken());
        return View::fetch();
    }

    public function logout()
    {
        $this->auth->logout();
        $this->success('退出成功');
    }

    public function password()
    {
        $password = $this->request->post('password/s', '');
        $this->auth->changePassword($password);
        $this->success('密码修改成功。');
    }

    public function echar()
    {
        $loginUser = $this->auth->getLoginUser();
        $property = PropertyModel::where('admin_user_id', $loginUser['id'])->select()->toArray();
        $result = array_map(function ($item) {
            return $item['id'];
        }, $property);
        $charData = array();
        for ($i=11; $i>=0; $i--) {
            $star = date("Y-m-01", strtotime("first day of -$i Months"));
            $end = date('Y-m-d', strtotime("$star +1 month -1 day"));
            $income = SumModel::where('house_property_id', 'in', $result)
            ->whereTime('accounting_date', 'between', [$star, $end])
            ->where('type', 'A')
            ->sum('total_money');
            $spending = SumModel::where('house_property_id', 'in', $result)
            ->whereTime('accounting_date', 'between', [$star, $end])
            ->where('type', 'in', ['B', 'C', 'D', 'E', 'F'])
            ->sum('total_money');
            \array_push($charData, ['month' => \substr($star, 0, 7), 'project'=> '收入', 'money'=>$income]);
            \array_push($charData, ['month' => \substr($star, 0, 7), 'project'=> '支出', 'money'=>intval($spending)]);
            \array_push($charData, ['month' => \substr($star, 0, 7), 'project'=> '利润', 'money'=>$income - intval($spending)]);
        }
        return $this->returnElement($charData);
    }
}
